#!/bin/sh

#######################################################################
# (1) run process from superuser root (less security)
# (0) run process from unprivileged user "nobody" (more security)
SVC_ROOT=1

# process prooryty (0-normal, 19-lowest)
SVC_PRIORITY=5

# access control list for transmission RPC. Please add actual address mask.
RPC_ACL="*.*.*.*"
#######################################################################

SVC_NAME="transmission-daemon"
SVC_PATH="/opt/bin/transmission-daemon"
DIR_CONF="/opt/etc/transmission-daemon"

func_start()
{
	# Make sure already running
	if [ -n "`pidof transmission-daemon`" ] ; then
		echo "$SVC_NAME already running!"
		return 0
	fi

	######################################
	LOGIN="your_login_here"
	MYSECRET="YoUr_SeCrEt"
	######################################

	if [ ! "$LOGIN" -o ! "$MYSECRET" ] ; then
		AUTH_INFO=""

		sed -i 's/\"rpc-authentication-required\"\:.*$/\"rpc-authentication-required\"\:\ false\,/' \
			"/opt/etc/transmission-daemon/settings.json" >/dev/null 2>&1
	else
		AUTH_INFO=" -t -u $LOGIN -v $MYSECRET"


	fi

	echo -n "Starting $SVC_NAME:."
	logger -t "$SVC_NAME" "Starting $SVC_NAME..."
	
	# first, try to found share/Download directory on both drives
	DIR_DL=""
	DIR_OK=""
	for i in "a1" "a2" "a3" "a4" "b1" "b2" "b3" "b4" ; do
		disk_path="/media/AiDisk_${i}"
		if [ -d "${disk_path}" ] && grep -q ${disk_path} /proc/mounts ; then
			[ -z "${DIR_OK}" ] && DIR_OK="${disk_path}"
			if [ -d "${disk_path}/share/Download" ] ; then
				DIR_DL="${disk_path}/share/Download"
				break
			fi
		fi
	done
	
	# if not found, use first mounted drive
	[ -z "${DIR_DL}" ] && DIR_DL="${DIR_OK}/share/Download"
	
	if [ -z "${DIR_DL}" ] ; then
		echo "[FAILED]"
		logger -t "$SVC_NAME" "Cannot start $SVC_NAME by reason - unable to find target drive!"
		return 1
	fi
	
	DIR_DL1="${DIR_DL}/Complete"
	DIR_DL2="${DIR_DL}/Incomplete"
	DIR_DL3="${DIR_DL}/Watch"
	DIR_LOG="/opt/var/log"
	
	[ ! -d "$DIR_CONF" ] && mkdir -p "$DIR_CONF"
	[ ! -d "$DIR_DL1" ]  && mkdir -p "$DIR_DL1"
	[ ! -d "$DIR_DL2" ]  && mkdir -p "$DIR_DL2"
	[ ! -d "$DIR_DL3" ]  && mkdir -p "$DIR_DL3"
	[ ! -d "$DIR_LOG" ]  && mkdir -p "$DIR_LOG"
	
	export TRANSMISSION_WEB_HOME="/opt/share/transmission/web"
	
	# full acces for watch dir
	chmod 777 "$DIR_DL3"
	
	# check start-stop-daemon stuff
	if [ -x /sbin/start-stop-daemon ] ; then
		if [ $SVC_ROOT -eq 0 ] ; then
			chown -R nobody "$DIR_CONF"
			chown -R nobody "$DIR_DL2"
			chmod -R 777 "$DIR_DL1"
			start-stop-daemon -S -N $SVC_PRIORITY -c nobody \
				-x $SVC_PATH -- -a "$RPC_ACL" \
				-g "$DIR_CONF" -w "$DIR_DL1" \
				-e ${DIR_LOG}/${SVC_NAME} \
				-x /var/run/${SVC_NAME}.pid $AUTH_INFO
		else
			start-stop-daemon -S -N $SVC_PRIORITY -x $SVC_PATH -- -a "$RPC_ACL" \
				-g "$DIR_CONF" -w "$DIR_DL1" \
				-e ${DIR_LOG}${SVC_NAME} \
				-x /var/run/${SVC_NAME}.pid ${AUTH_INFO}
		fi
	else
		$SVC_PATH -a "$RPC_ACL" -g "$DIR_CONF" 
			-w "$DIR_DL1" -e ${DIR_LOG}/${SVC_NAME} 
			-x /var/run/${SVC_NAME}.pid ${AUTH_INFO}
	fi
	
	if [ $? -eq 0 ] ; then
		echo "[  OK  ]"
	else
		echo "[FAILED]"
	fi
}

func_stop()
{
	# Make sure not running
	if [ -z "`pidof transmission-daemon`" ] ; then
		echo "$SVC_NAME not running!"
		return 0
	fi
	
	echo -n "Stopping $SVC_NAME:."
	logger -t "$SVC_NAME" "Stopping $SVC_NAME..."
	
	# stop daemon
	killall -q transmission-daemon
	
	# gracefully wait max 20 seconds while transmission stopped
	i=0
	while [ -n "`pidof transmission-daemon`" ] && [ $i -le 20 ] ; do
		echo -n "."
		i=$(( $i + 1 ))
		sleep 1
	done
	
	trmd_pid=`pidof transmission-daemon`
	if [ -n "$trmd_pid" ] ; then
		# force kill (hungup?)
		kill -9 "$trmd_pid"
		sleep 1
		echo "[KILLED]"
		logger -t "$SVC_NAME" "Cannot stop $SVC_NAME by reason - timeout!"
	else
		echo "[  OK  ]"
	fi
}

func_reload()
{
	trmd_pid=`pidof transmission-daemon`
	if [ -n "$trmd_pid" ] ; then
		echo -n "Reload $SVC_NAME config:."
		kill -1 "$trmd_pid"
		echo "[  OK  ]"
	else
		echo "Error: $SVC_NAME is not started!"
	fi
}

case "$1" in
start)
	func_start
	exit 0
	;;
stop)
	func_stop
	exit 0
	;;
reload)
	func_reload
	exit 0
	;;
restart)
	func_stop
	func_start
	exit 0
	;;
*)
	echo "Usage: $0 {start|stop|reload|restart}"
	exit 1
	;;
esac

exit 2
